package com.taiyi.crm.controller;

import com.taiyi.crm.pojo.bo.SaleChanceSearchBO;
import com.taiyi.crm.pojo.entity.Customer;
import com.taiyi.crm.pojo.entity.Product;
import com.taiyi.crm.pojo.entity.SaleChance;
import com.taiyi.crm.pojo.entity.User;
import com.taiyi.crm.service.ICustomerService;
import com.taiyi.crm.service.IProductService;
import com.taiyi.crm.service.ISaleChanceService;
import com.taiyi.crm.service.IUserService;
import com.taiyi.crm.util.JSONResult;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@ApiIgnore
@RequestMapping("/saleChance")
@Controller
public class SaleChanceController {
    @Autowired
    private ISaleChanceService saleChanceService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/getSaleChancePage")
    public String getSaleChancePage() {
        return "/sale_chance/sale_chance_list";
    }

    // http://localhost:8080/saleChance/selectByPage?page=1&limit=10&chanceSource=电话&devResult=0
    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(Integer page, Integer limit, SaleChanceSearchBO saleChanceSearchBO) {
        return saleChanceService.list(page, limit, saleChanceSearchBO);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        saleChanceService.deleteById(id);
        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/getInsertPage")
    public String getInsertPage(Model model) {
        System.out.println("SaleChanceController.getInsertPage");
        List<Customer> customerList = customerService.selectAll();
        model.addAttribute("customerList", customerList);
        List<Product> productList = productService.selectAll();
        model.addAttribute("productList", productList);
        List<User> userList = userService.selectXiaoShouUser();
        model.addAttribute("userList", userList);
        return "/sale_chance/sale_chance_insert";
    }

    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(SaleChance saleChance) {
        System.out.println(saleChance);
        saleChanceService.insert(saleChance);
        return  JSONResult.ok("添加成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(SaleChance saleChance) {
        System.out.println(saleChance);
        saleChanceService.update(saleChance);
        return JSONResult.ok("更新成功");
    }

    @RequestMapping("/getUpdatePage")
    public String getUpdatePage(Model model) {
        System.out.println("SaleChanceController.getUpdatePage");
        List<Customer> customerList = customerService.selectAll();
        model.addAttribute("customerList", customerList);
        List<Product> productList = productService.selectAll();
        model.addAttribute("productList", productList);
        List<User> userList = userService.selectXiaoShouUser();
        model.addAttribute("userList", userList);
        return "/sale_chance/sale_chance_update";
    }

}

